dojo.provide("uws.drawing.PropertiesManager");

dojo.declare('uws.drawing.PropertiesManager',null,{
	/*
		It responds to the properties broadcasted by ComponentFocusManager and displays the properties. It can then be altered.
	*/
	id:null,
	currentItem:null,
	
	constructor:function(){
		dojo.require("dijit.ColorPalette");
		dojo.require("dijit.form.NumberTextBox");
		dojo.require("dijit.form.Textarea");
		dojo.require("dijit.ColorPalette");
		dojo.require("dijit.form.CheckBox");
		dojo.require("dijit.form.NumberTextBox");
		dojo.require("dijit.form.Textarea");
		dojo.addOnLoad(this._subscribe);
	},
	_subscribe:function(){
		//subscribe to publish properties
		dojo.subscribe("uwsProperties",function(args){
			var attr = args.attributes;
			var val = args.values;
			var uwsType = "";
			var umlType = "";
			var propertiesDiv = dojo.byId("propertiesDiv");
			var innerHtml = '<table style="width:100%">';
			
			for(var i=0; i<attr.length; i++){
				if(attr[i] == "custom" && val[i].length>6){
					var customAttr = val[i].split("|");
					for(var j=0; j< customAttr.length; j++){
						
						if(trim(customAttr[j]).length==0){
							continue;
						}
						
						var customItem = customAttr[j].split("#");
						
						if(customItem[4] == "Boolean"){
							var checked = (customItem[2]=="true") ? 'checked' : '';
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><input type="checkbox" dojoType="dijit.form.CheckBox" name="'+customItem[0]+'" '+checked+'" value="true" /></td></tr>';
						}else if(customItem[4] == "Text"){
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><textarea name="'+customItem[0]+'" dojoType="dijit.form.Textarea">'+customItem[2]+'</textarea></td></tr>';
						}else if(customItem[4]== "String"){
							var isRequired = false;
							if(customItem[3] == "true") isRequired = true;
							
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><input type="text" name="'+customItem[0]+'" dojoType="dijit.form.ValidationTextBox" required="'+isRequired+'" value="'+customItem[2]+'" /></td></tr>';
						}else if(customItem[4] == "Integer"){
							var isRequired = false;
							if(customItem[3] == "true") isRequired = true;
							
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><input type="text" name="'+customItem[0]+'" dojoType="dijit.form.NumberTextBox"  constraints="{places:0} required="'+isRequired+'" value="'+customItem[2]+'" /></td></tr>';
						}else if(customItem[4] == "Decimal"){
							var isRequired = false;
							if(customItem[3] == "true") isRequired = true;
							
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><input type="text" name="'+customItem[0]+'" dojoType="dijit.form.NumberTextBox" required="'+isRequired+'" value="'+customItem[2]+'" /></td></tr>';
						}else if(customItem[4] == "Color"){
							var isRequired = false;
							if(customItem[3] == "true") isRequired = true;
							
							innerHtml += '<tr><td>'+customItem[1]+'</td>';
							innerHtml += '<td><input type="hidden" name="'+customItem[0]+'" id="colorPaletteProperty" value="'+customItem[2]+'" />' + 
											'<div dojoType="dijit.ColorPalette" onChange="alert(this.value);"></div></td></tr>';
						}
					}
				}else if(
					attr[i] == "x" || attr[i] == "y"  || attr[i] == "sortOrder" ||
					attr[i] == "connections" || attr[i] == "children" ||
					attr[i] == "ref1_id" || attr[i] == "ref2_id" || attr[i] == "diagramType" ||
					attr[i] == "comp_children" || attr[i] == "ref1_dir" || attr[i] == "ref2_dir"
				){
					//ignore
				}else if(attr[i] == "label"){
					if(surfaceManager.getVisibleSurface().type=="classDiagram"){
						if(propertiesManager.currentItem=="classBox"){
							innerHtml += '<tr><td>Class name</td><td><input type="text" ';
							innerHtml += 'dojoType="dijit.form.TextBox" ';
							innerHtml += 'name="'+attr[i]+'" value="'+val[i]+'" /></td></tr>';
						}else{
							innerHtml += '<tr><td>'+attr[i]+'</td><td><input type="text" ';
							innerHtml += 'dojoType="dijit.form.TextBox" ';
							innerHtml += 'name="'+attr[i]+'" value="'+val[i]+'" /></td></tr>';
						}
					}else{
						innerHtml += '<tr><td>'+attr[i]+'</td><td><input type="text" ';
						innerHtml += 'dojoType="dijit.form.TextBox" ';
						innerHtml += 'name="'+attr[i]+'" value="'+val[i]+'" /></td></tr>';
					}
				}else if(attr[i] == "description"){
					innerHtml += '<tr><td>description</td><td><textarea name="description" dojoType="dijit.form.Textarea">'+val[i]+'</textarea></td></tr>';
				}else if(attr[i] == "width" || attr[i] == "height"){
					innerHtml += '<tr><td>'+attr[i]+'</td><td><input type="text" ';
					innerHtml += 'dojoType="dijit.form.NumberTextBox" required="true" ';
					innerHtml += 'name="'+attr[i]+'" value="'+val[i]+'" constraints="{min:100,places:0}" /></td></tr>';
				}else if(
					attr[i] == "id" ||
					attr[i] == "umltype" ||
					attr[i] == "uwstype" ||
					attr[i] == "custom"
				){
					if(attr[i] == "id") propertiesManager.currentItem = val[i];
					
					if(attr[i]=="umltype"){
						innerHtml += '<tr><td>UML Type</td><td>'+val[i]+'</td></tr>';
						umlType = val[i];
					}
					
					if(attr[i]=="uwstype"){
						innerHtml += '<tr><td>UWS Type</td><td>'+val[i]+'</td></tr>';
						uwsType = val[i];
					}
					
					innerHtml += '<tr><td></td><td><input type="hidden" name="'+attr[i]+'" dojoType="dijit.form.TextBox" value="'+val[i]+'" /></td></tr>';
				}else{
					innerHtml += '<tr><td>'+attr[i]+'</td><td><input type="text" ';
					innerHtml += 'dojoType="dijit.form.TextBox" ';
					innerHtml += 'name="'+attr[i]+'" value="'+val[i]+'" /></td></tr>';
				}
			}
			innerHtml +='</table>';
			
			if(uwsType=="component" && umlType == "classBox"){
				innerHtml += "<button value='Add New Child' dojoType='dijit.form.Button' onClick='childrenManager.showNewChildrenDialog(\""+propertiesManager.currentItem+ "\")'>Add New Child</button>";
				innerHtml += "<br /><br /><div id='propertiesChildrenDiv'></div>";
			}
			var childrenHTML = "";
			
			dojo.byId("propertiesName").innerHTML = val[1];
			propertiesDiv.innerHTML = innerHtml;
			dojo.parser.parse(dojo.byId('propertiesDiv'));
			
			if(uwsType=="component" && umlType == "classBox"){
				
				childrenHTML += "<table border='1' cellspacing='0' cellpadding='5'>";
				
				dataStore.fetchItemByIdentity({
					identity: propertiesManager.currentItem,
					scope: this,
					onItem:function(item){
						var children = dataStore.getValues(item, "comp_children");
						
						for(var i=0; children!=null && children!=undefined && i<children.length; i++){
							var childId = dataStore.getValue(children[i], "id");
							
							childrenHTML += "<tr><td>"+ dataStore.getValue(children[i], "type") +"</td><td>"+dataStore.getValue(children[i], "name")+"</td><td>"+
							"<a href='javascript:childrenManager.showEditChildrenDialog(\""+childId+"\",\""+propertiesManager.currentItem+"\")'><img src='images/edit.jpg' alt='edit' title='Edit' style='width:20px' /></a> <a href='javascript:childrenManager.deleteChild(\""+childId+"\")'><img src='images/delete.jpg' alt='delete' title='Delete' style='width:20px' /></a></td></tr>";
						}
						
						childrenHTML += "</table>";
						
					}
				});
				childrenHTML += "</table>";
				dojo.byId('propertiesChildrenDiv').innerHTML = childrenHTML;
			}
		});
	},
	applyProperties:function(){
		if(propertiesForm && !propertiesForm.isValid()) return;
		
		(function(){
			var values = propertiesForm.get('value');
			
			if(values.uwstype=="component"){
				dataStore.fetchItemByIdentity({
					identity: values.id,
					scope:this,
					onItem:function(item){
						var custom = "";
						
						for (var name in values) {
							if(
								(
									name != "id" &&
									name != "name" &&
									name != "uwstype" &&
									name != "umltype" &&
									name != "x" &&
									name != "y" &&
									name != "label" &&
									name != "description"
								) &&
								values.hasOwnProperty(name)
							) {
								custom += rebuildCustomAttribute(item, name, values[name]);
							}
						}
						
						values.custom = custom;
						
						dataStore.setValue(item,"name",values.name);
						dataStore.setValue(item,"label",values.label);
						dataStore.setValue(item,"description",values.description);
						dataStore.setValue(item,"custom",values.custom);
						
						if(values.uwstype == "component") componentManager.components[values["id"]].applyProperties(values);
					},
					onError:function(error){
						console.debug(error);
					}
				});
			}else if(values.uwstype=="diagram"){
				dataStore.fetchItemByIdentity({
					identity: values.id,
					scope:this,
					onItem:function(item){
						dataStore.setValue(item,"name",values.name);
						dataStore.setValue(item,"width",values.width);
						dataStore.setValue(item,"height",values.height);
						surfaceManager.getSurface(values.id).setDimensions(values.width,values.height);
					},
					onError:function(e){
						console.debug(e);
					}
				});
			}else if(values.uwstype=="connection"){
				dataStore.fetchItemByIdentity({
					identity: values.id,
					scope:this,
					onItem:function(item){
						var custom = "";
						
						for (var name in values) {
							if(
								(
									name != "id" &&
									name != "name" &&
									name != "uwstype" &&
									name != "umltype" &&
									name != "x" &&
									name != "y" &&
									name != "label" &&
									name != "cardinality1" &&
									name != "cardinality2" &&
									name != "stereotype" 
								) &&
								values.hasOwnProperty(name)
							) {
								custom += rebuildCustomAttribute(item, name, values[name]);
							}
						}
						values.custom = custom;
						
						dataStore.setValue(item,"name",values.name);
						dataStore.setValue(item,"stereotype",values.stereotype);
						dataStore.setValue(item,"description",values.description);
						dataStore.setValue(item,"cardinality1", values.cardinality1);
						dataStore.setValue(item,"cardinality2", values.cardinality2);
						dataStore.setValue(item,"custom", values.custom);
						
						connectionManager.getById(values.id).applyProperties(values);
					},
					onError:function(e){
						console.debug(e);
					}
				});
			}
		}());
	},
	deleteItem:function(){
		//clear the propertiesDiv
		var values = propertiesForm.get('value');
		dojo.byId("propertiesName").innerHTML = '';
		dojo.byId("propertiesDiv").innerHTML = '<span style="font-style:italic">Please select an item.</span>';
		propertiesManager.currentItem = null;
		
		if(values.uwstype=="diagram"){
			surfaceManager.destroyItem(values.id, false);
		}else if(values.uwstype=="component"){
			componentManager.destroyItem(values.id);
		}else if(values.uwstype=="connection"){
			connectionManager.destroyItem(values.id);
		}
	}
});